import { get, MsgService, post } from "./base"

interface CreateReq {
    type_id: number,
    mac_addr: string,
    name: string,
    remark: string
}

export async function createDevice(req: CreateReq) {
    await post('/device/create', req);
}

export interface TbDevice {
    id: number,
    mac_addr: string,
    name: string,
    remark: string,
}

export async function queryDevice(type_id: number) {
    const resp = await get(`/device/query?type_id=${type_id}`);
    return await resp.parse<TbDevice[]>();
}

interface UpdateReq {
    id: number,
    name: string,
    remark: string,
}

export async function updateDevice(req: UpdateReq) {
    await post('/device/update', req);
}

export async function deleteDevice(id: number) {
    await post('/device/delete', id);
}

export async function request<T>(id: number, cmd: string, timeout: number, body?: T | Uint8Array) {
    return await MsgService.post('/device/req', body, {
        'id': `${id}`,
        'cmd': cmd,
        'timeout': `${timeout}`
    });
}

export async function notify<T>(id: number, cmd: string, body?: T | Uint8Array) {
    return await MsgService.post('/device/notify', body, {
        'id': `${id}`,
        'cmd': cmd,
    });
}

export async function pollTimestamp(id: number) {
    return await MsgService.post('/device/poll_timestamp', null, {
        'id': `${id}`,
    });
}

